package com.boco.pciv.core.entity;

import cn.hutool.core.util.StrUtil;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by CC on 2017/4/26.
 */
public class CriteriaBase {
    private List<CriteriaItem> criteria = new ArrayList<>();
    public void putCriteria(String key,Object value){
        putCriteria(key,value,CriteriaEnum.Equals);
    }
    public void putCriteria(String key,Object value,CriteriaEnum type){
        if (StrUtil.isBlank(key)){
            return;
        }
        //允许重复
        criteria.add(new CriteriaItem(key,value,type));
    }
    public List<CriteriaItem> getCriteria(){
        return criteria;
    }

    public void removeCriteriaByKey(String key){
        Optional<CriteriaItem> item = getCriteriaByKey(key);
        item.ifPresent(criteriaItem -> criteria.remove(criteriaItem));
    }
    public Optional<CriteriaItem> getCriteriaByKey(String key){
        Optional<CriteriaItem> item = criteria.stream().filter(p -> p.getKey().equalsIgnoreCase(key)).findFirst();
        return item;
    }

    /**
     * 获取调解的map 只读
     * @return
     */
    public Map<String,Object> getCondMap(){
        //重复key使用key2
        return criteria.stream()
                .collect(Collectors.toMap(CriteriaItem::getKey,CriteriaItem::getVal,(key1,key2)->key2));
    }
}
